package com.rc.evcharger.common.terminal;


import com.rc.evcharger.common.utils.DataUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 读当前数据返回的结果数据域格式
 *
 * @author kb
 */
public class CurrDataResult implements Result {
    private String cardNo; // 用户卡号
    private String code; // 数据项目码
    private String value; // 对应的值
    private String gunNum; // 枪号
    private String source;  //数据来源（硬件产商）

    public CurrDataResult(String source) {
        this.source = source;
    }

    public CurrDataResult(String code, String value, String gunNum) {
        super();
        this.code = code;
        this.value = value;
        this.gunNum = gunNum;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getGunNum() {
        return gunNum;
    }

    public void setGunNum(String gunNum) {
        this.gunNum = gunNum;
    }

    public String getCardNo() {
        return cardNo;
    }

    public void setCardNo(String cardNo) {
        this.cardNo = cardNo;
    }

    @Override
    public List<CurrDataResult> getResult(String dataStr) {
        List<CurrDataResult> result = new ArrayList<>();
        dataStr = dataStr.trim();
        // 测量点
        String measuringPoint = dataStr.substring(0, 23);
        // 获取枪口
        String gunNum = DataUtil.getGunNum(measuringPoint.substring(0, 2), this.source);

        dataStr = dataStr.substring(24);
        // 获取数据编码
        while (dataStr.length() > 6) {
            String code = dataStr.substring(0, 5);
            dataStr = dataStr.substring(6);
            for (Object[] dataCode : DataUtil.dataCodes) {
                if (code.equals(dataCode[0])) { // 判断数据编码是哪一个然后根据对应的数据长度截取数据
                    int length = (int) dataCode[1];
                    length = length * 2 + length - 1;
                    String value = dataStr.substring(0, length);
                    if (dataStr.length() > length + 1)
                        dataStr = dataStr.substring(length + 1);
                    result.add(new CurrDataResult(code, value, gunNum));
                }
            }
        }
        setCardNo(result);
        return result;
    }

    /**
     * 如果返回的一帧数据中有卡号，就要将卡号放进每一个数据项里    FF FF FF代表没有
     *
     * @param list
     */
    private void setCardNo(List<CurrDataResult> list) {
        String cardNo = null;
        for (CurrDataResult cdr : list) {
            if (cdr.getCode().equals(DataUtil.dataCodes[19][0].toString())) {
                //if (!cdr.getValue().contains("FF FF FF")) {
                cardNo = cdr.getValue();
                break;
                //}
            }
        }
        for (CurrDataResult cdr : list) {
            if (cardNo == null) {
                cdr.setCardNo(cardNo);
            } else if (cardNo.contains("FF FF FF")) {
                if (cdr.getCode().equals(DataUtil.dataCodes[14][0].toString())) {
                    cdr.setCardNo(cardNo);
                }
            } else {
                cdr.setCardNo(cardNo);
            }
        }
    }
}
